package com.music.controller.music;

import com.alibaba.fastjson.JSONObject;
import com.music.dto.system.TokenDTO;
import com.music.dto.utils.OpResultDTO;
import com.music.service.music.SelfCenterService;
import com.music.utils.HttpRequestReader;
import com.music.utils.JWTUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/api/selfCenter")
public class SelfCenterController {
    @Resource
    private SelfCenterService selfCenterService;
    private final Logger logger = LoggerFactory.getLogger(com.music.controller.music.SelfCenterController.class);

    /**
     * @Description: 获取用户信息
     * @Author hbh
     * @Date 2021/12/26
     */
    @RequestMapping(value = "/getInfo", method = RequestMethod.POST)
    public OpResultDTO getInfo(@RequestHeader(name = "Access-Token") String token) {
        OpResultDTO opResult = new OpResultDTO();
        try {
            TokenDTO tokenDTO = JWTUtil.verifyToken(token);
            opResult.setResult(selfCenterService.getInfo(tokenDTO.getAccountId()));
        } catch (Exception e) {
            logger.error(e.toString());
            opResult.setMessage("error");
        }
        return opResult;
    }

    /**
     * @Description: 获取用户信息
     * @Author hbh
     * @Date 2021/12/26
     */
    @RequestMapping(value = "/getSingerName", method = RequestMethod.POST)
    public OpResultDTO getSingerName(@RequestHeader(name = "Access-Token") String token, HttpServletRequest request) {
        OpResultDTO opResult = new OpResultDTO();
        try {
            TokenDTO tokenDTO = JWTUtil.verifyToken(token);
            JSONObject jsonObject = HttpRequestReader.getJsonObject(request);
            Long userId;
            if (jsonObject!=null) {
                userId = jsonObject.getLong("userId");
            } else {
                userId = tokenDTO.getAccountId();
            }
            opResult.setResult(selfCenterService.getSingerName(userId));
        } catch (Exception e) {
            logger.error(e.toString());
            opResult.setMessage("error");
        }
        return opResult;
    }

    /**
     * @Description: 获取用户信息
     * @Author hbh
     * @Date 2021/12/26
     */
    @RequestMapping(value = "/getDownloadCount", method = RequestMethod.POST)
    public OpResultDTO getDownloadCount(@RequestHeader(name = "Access-Token") String token, HttpServletRequest request) {
        OpResultDTO opResult = new OpResultDTO();
        try {
            TokenDTO tokenDTO = JWTUtil.verifyToken(token);
            JSONObject jsonObject = HttpRequestReader.getJsonObject(request);
            Long userId;
            if (jsonObject!=null) {
                userId = jsonObject.getLong("userId");
            } else {
                userId = tokenDTO.getAccountId();
            }
            opResult.setResult(selfCenterService.getDownloadCount(userId));
        } catch (Exception e) {
            logger.error(e.toString());
            opResult.setMessage("error");
        }
        return opResult;
    }
}
